//
//  BlockQueue.m
//  IMChezhil
//
//  Created by zhangliucheng on 15/7/23.
//  Copyright (c) 2015年 CheZhil. All rights reserved.
//

#import "IMBlockQueue.h"

@interface IMBlockQueue ()

@property (strong, nonatomic) NSMutableArray *data;

@end

@implementation IMBlockQueue

- (instancetype)init {
    self = [super init];
    if (self) {
        self.data = [NSMutableArray new];
    }
    return self;
}

- (void)addFirst:(id)obj {
    if (obj) {
        [self.data insertObject:obj atIndex:0];
    }
}

- (void)offer:(id)obj {
    if (obj) {
        [self.data addObject:obj];
    }
}

- (id)peek {
    return [self.data firstObject];
}

- (id)poll:(NSTimeInterval)interval {
    id obj = [self.data firstObject];
    if (obj) {
        [self.data removeObject:obj];
        return obj;
    }
    sleep(interval);
    
    id objL = [self.data firstObject];
    if (objL) {
        [self.data removeObject:objL];
    }
    return objL;
}

- (NSInteger)count {
    return [self.data count];
}
@end
